package ngames.com.sushitv.action;

import android.app.Activity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.adapter.AnimeIntroAdapter;
import ngames.com.sushitv.adapter.AnimeSingleVideoAdapter;
import ngames.com.sushitv.adapter.VideoRankAdapter;
import ngames.com.sushitv.base.BaseListAction;
import ngames.com.sushitv.core.ModelCallback;
import ngames.com.sushitv.entity.Anime;
import ngames.com.sushitv.entity.AnimeIntro;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.entity.UpUser;
import ngames.com.sushitv.entity.VideoBase;
import ngames.com.sushitv.entity.VideoDetails;
import ngames.com.sushitv.entity.genres;
import ngames.com.sushitv.view.FlowLayout;

/**
 * Created by Jeffrey on 2016/7/27.
 */
public class AnimeIntroAction extends BaseListAction {
    private TextView titleTxt,continueTxt, totalSeriesTxt,lastEpisodeTxt,genresTxt, tagsTxt, introTxt;
    private RecyclerView recyclerView;
    private ImageView upImg;
    private String genresStr="";
    public AnimeIntroAction(Activity activity, View baseView) {
        super(activity, baseView);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        titleTxt = (TextView) mBaseView.findViewById(R.id.title);
        continueTxt= (TextView) mBaseView.findViewById(R.id.continue_button);
        lastEpisodeTxt= (TextView) mBaseView.findViewById(R.id.text_lastEpisode);
        genresTxt= (TextView) mBaseView.findViewById(R.id.text_genres);
        tagsTxt= (TextView) mBaseView.findViewById(R.id.tags);
        //shareTxt = (TextView) mBaseView.findViewById(R.id.text_share);

        //favTxt = (TextView) mBaseView.findViewById(R.id.text_fav);
       // rewardTxt = (TextView) mBaseView.findViewById(R.id.text_reward);
        introTxt = (TextView) mBaseView.findViewById(R.id.text_summary);
        introTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                introTxt.setMaxLines(Integer.MAX_VALUE);
            }
        });
        totalSeriesTxt= (TextView) mBaseView.findViewById(R.id.text_totalSeries);

        //upnameTxt = (TextView) mBaseView.findViewById(R.id.name_main);
        //uploadsTxt = (TextView) mBaseView.findViewById(R.id.uploads_count);
        //followersTxt = (TextView) mBaseView.findViewById(R.id.followers_count);
        upImg = (ImageView) mBaseView.findViewById(R.id.cover);

        recyclerView = (RecyclerView) mBaseView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(mActivity, 7));



        onRefreshData();
    }

    private void initData() {
        Log.e("request", AppApplication.getmVsId());
        getAppModel().animeDetails(AppApplication.getmVsId(), new ModelCallback<ArrayList<ItemVideo>>() {
            @Override
            public void onBeforeRequest(String urlTag) {
            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> data) {
                Log.e("anime", "anime is ok");
                setData();
            }

            @Override
            public void onFailure(String errorEvent, String message) {

            }
        });
    }

    private void setData() {
        AnimeIntro animeIntro=AppApplication.getmAnimeIntro();
        Anime anime=animeIntro.getAnime();
        ArrayList<genres> ge=animeIntro.getGenres();
        ArrayList<ItemVideo> series=animeIntro.getSeries();
        titleTxt.setText(anime.getTitle());
        lastEpisodeTxt.setText("Update to EP "+anime.getLast_episode());
        //shareTxt.setText(vb.getShare_count());
        //favTxt.setText(vb.getFav_count());
        //rewardTxt.setText(vb.getReward_count());
        introTxt.setText(anime.getSummary());
        continueTxt.setText(anime.getComplete_status());
        totalSeriesTxt.setText("Episodes "+anime.getTotal_series());
//        upnameTxt.setText(up.getName());
//        uploadsTxt.setText("Uploads: " + up.getUploads());
//        followersTxt.setText("Followers: " + up.getFollows());

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                    /*.showStubImage(R.drawable.image_is_loading)            // 设置图片下载期间显示的图片
                    .showImageForEmptyUri(R.drawable.image_loading_failed)    // 设置图片Uri为空或是错误的时候显示的图片
                    .showImageOnFail(R.drawable.image_loading_failed)        // 设置图片加载或解码过程中发生错误显示的图片
                   */
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)                            // 设置下载的图片是否缓存在SD卡中
                .build();
        String workerImgStr = anime.getCover();
        ImageLoader.getInstance().displayImage(workerImgStr, upImg, options);



        AnimeIntroAdapter introAdapter = new AnimeIntroAdapter(mActivity, series);

        recyclerView.setAdapter(introAdapter);
        for(int i=0;i<(ge==null?0:ge.size());i++){
            genres g=ge.get(i);
            genresStr+="  "+g.getName();
        }
        genresTxt.setText("Views"+genresStr);
        tagsTxt.setText("Ongoing |"+genresStr);
//        VideoRankAdapter adapter = new VideoRankAdapter(mActivity, v.getRecommends());
//        //FanMadeIntroVideoAdapter adapter = new FanMadeIntroVideoAdapter(mActivity, fanMadeVideos);
//        List<String> strings = new ArrayList<>();
//        strings.add("Release Time");
//        strings.add("Video");
//        strings.add("Comments");
//        strings.add("Danmaku");
//
//        FlowLayout flowLayout = (FlowLayout) mBaseView.findViewById(R.id.flow_layout);
//        flowLayout.setIfHide(false);
//        flowLayout.setText(strings);
//        AnimeSingleVideoAdapter adapter1 = new AnimeSingleVideoAdapter(mActivity, v.getSeries());
//
//        recyclerView1.setAdapter(adapter);
//        recyclerView2.setAdapter(adapter1);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefreshData() {
        initData();
    }

    @Override
    public void loadMore() {
//        videoRank();
    }


}
